package sunyu.demo.storm.kafka;

import org.apache.kafka.clients.consumer.Consumer;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.storm.spout.SpoutOutputCollector;
import org.apache.storm.task.TopologyContext;
import org.apache.storm.topology.IRichSpout;
import org.apache.storm.topology.OutputFieldsDeclarer;
import org.apache.storm.tuple.Fields;
import org.apache.storm.tuple.Values;
import org.slf4j.Logger;
import sunyu.toolkit.core.ExceptionKit;
import sunyu.toolkit.core.LogKit;

import java.util.Arrays;
import java.util.Map;
import java.util.Properties;

/**
 * @author 孙宇
 */
public class MyKafkaSpout implements IRichSpout {

    private static final Logger logger = LogKit.getLogger();
    public String servers = "127.0.0.1:9092";
    public String keyDeserializer = "org.apache.kafka.common.serialization.StringDeserializer";
    public String valueDeserializer = "org.apache.kafka.common.serialization.StringDeserializer";
    public String topic = "test.topic";
    public String group = "test.group";
    private SpoutOutputCollector collector;
    private boolean runner = true;

    @Override
    public void open(Map conf, TopologyContext context, SpoutOutputCollector collector) {
        this.collector = collector;
        this.servers = String.valueOf(conf.get("kafkaServers"));
        this.topic = String.valueOf(conf.get("kafkaTopic"));
        this.group = String.valueOf(conf.get("kafkaGroup"));
    }

    @Override
    public void close() {
        runner = false;
    }

    @Override
    public void activate() {
    }

    @Override
    public void deactivate() {
    }

    @Override
    public void nextTuple() {
        Properties props = new Properties();
        props.put("bootstrap.servers", servers);
        props.put("key.deserializer", keyDeserializer);
        props.put("value.deserializer", valueDeserializer);
        props.put("group.id", group);//不同ID 可以同时订阅消息
        try (Consumer<String, String> consumer = new org.apache.kafka.clients.consumer.KafkaConsumer<>(props);) {
            consumer.subscribe(Arrays.asList(topic.split(",")));//订阅多个TOPIC
            while (runner) {
                ConsumerRecords<String, String> records = consumer.poll(Long.MAX_VALUE);
                for (ConsumerRecord<String, String> record : records) {
                    logger.debug("topic = {}, partition = {}, offset = {}, key = {}, value = {}", record.topic(), record.partition(), record.offset(), record.key(), record.value());
                    this.collector.emit(new Values(record));
                }
            }
        } catch (Exception e) {
            logger.error(ExceptionKit.getStackTrace(e));
        }
    }

    @Override
    public void ack(Object msgId) {
    }

    @Override
    public void fail(Object msgId) {
    }

    @Override
    public void declareOutputFields(OutputFieldsDeclarer declarer) {
        declarer.declare(new Fields("msg"));
    }

    @Override
    public Map<String, Object> getComponentConfiguration() {
        return null;
    }
}
